//
//  HWTakeALookDetailCommentFooter.swift
//  PlayWithProject
//
//  Created by 王昌健 on 2021/8/4.
//

import UIKit

class HWTakeALookDetailCommentFooter: UICollectionReusableView {
    
    var lineView:UIView!
    var cellOpenButton:UIButton!
    var arrowImageView:UIImageView!
    
    var count:NSInteger = 0
    var cellOpenClick:((_:HWTakeALookCommentModel)->())?
    var model:HWTakeALookCommentModel?{
        didSet{
            if count > 2 {
                cellOpenButton.isHidden = false
                lineView.isHidden = false
                arrowImageView.isHidden = false

                if model?.isOpen == true {
                    cellOpenButton.setTitle("收起", for: UIControl.State.normal)
                    let image = UIImage(named: "openArrow")
                    arrowImageView.image = UIImage.init(cgImage: (image?.cgImage)!, scale: image!.scale, orientation: UIImage.Orientation.down)
                }else{
                    cellOpenButton.setTitle("查看全部"+"\(count)" + "条回复", for: UIControl.State.normal)
                    arrowImageView.image = UIImage(named: "openArrow")
                    
                }
                
            }else{
                cellOpenButton.isHidden = true
                lineView.isHidden = true
                arrowImageView.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initUI()
        
        initFrame()
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension HWTakeALookDetailCommentFooter {
    private func initUI() -> Void {
        
        lineView = UIView()
        lineView.backgroundColor = .colorWithHexString(hexString: "#E6E7E8")
        addSubview(lineView)
        
        cellOpenButton = UIButton(type: UIButton.ButtonType.custom)
        cellOpenButton.setTitle("展开", for: UIControl.State.normal)
        cellOpenButton.setTitleColor(.colorWithHexString(hexString: "#999999"), for: UIControl.State.normal)
        cellOpenButton.titleLabel?.font = UIFont.boldSystemFont(ofSize: 12)
        cellOpenButton.addTarget(self, action: #selector(cellOpenButtonClick), for: UIControl.Event.touchUpInside)
        addSubview(cellOpenButton)
        
        arrowImageView = UIImageView()
        arrowImageView.image = UIImage(named: "openArrow")
        addSubview(arrowImageView)
    }
    
    private func initFrame() -> Void {
        lineView.snp.makeConstraints { make in
            make.left.equalTo(70)
            make.height.equalTo(1)
            make.width.equalTo(25)
            make.centerY.equalTo(cellOpenButton)
        }
        
        cellOpenButton.snp.makeConstraints { make in
            make.left.equalTo(lineView.snp.right).offset(5)
            make.top.equalTo(10)
        }
        
        arrowImageView.snp.makeConstraints { make in
            make.left.equalTo(cellOpenButton.snp.right).offset(5)
            make.centerY.equalTo(cellOpenButton)
        }
    }
}

extension HWTakeALookDetailCommentFooter {
    @objc func cellOpenButtonClick() -> Void {
//        model?.isOpen = !(model?.isOpen ?? false)
        self.cellOpenClick!(model!)
    }
}
